package com.hamizhong.iflowers.service.impl;

import java.util.List;

import org.springframework.beans.BeanUtils;

import com.hamizhong.iflowers.db.dao.hibernate.IGoodsDao;
import com.hamizhong.iflowers.db.dao.hibernate.IGoodsPurposeMapDao;
import com.hamizhong.iflowers.db.dao.hibernate.IPurposeDao;
import com.hamizhong.iflowers.db.entity.Goods;
import com.hamizhong.iflowers.db.entity.GoodsPurposeMap;
import com.hamizhong.iflowers.db.entity.Purpose;
import com.hamizhong.iflowers.service.IGoodsPurposeMapService;
import com.hamizhong.iflowers.service.base.MyBaseService;
import com.hamizhong.iflowers.utils.MyErrorMsgConstants;
import com.hamizhong.iflowers.web.dto.IOPResultDto;
import com.hamizhong.iflowers.web.vo.GoodsPurposeMapVO;
import com.hamizhong.iflowers.web.vo.IObjectVO;

public class GoodsPurposeMapServiceImpl extends MyBaseService implements IGoodsPurposeMapService {
	private IGoodsDao iGoodsDao = null;
	private IPurposeDao iPurposeDao = null;
	private IGoodsPurposeMapDao iGoodsPurposeMapDao = null;
	
	public IGoodsDao getiGoodsDao() {
		return iGoodsDao;
	}
	public void setiGoodsDao(IGoodsDao iGoodsDao) {
		this.iGoodsDao = iGoodsDao;
	}
	public IPurposeDao getiPurposeDao() {
		return iPurposeDao;
	}
	public void setiPurposeDao(IPurposeDao iPurposeDao) {
		this.iPurposeDao = iPurposeDao;
	}
	public IGoodsPurposeMapDao getiGoodsPurposeMapDao() {
		return iGoodsPurposeMapDao;
	}
	public void setiGoodsPurposeMapDao(IGoodsPurposeMapDao iGoodsPurposeMapDao) {
		this.iGoodsPurposeMapDao = iGoodsPurposeMapDao;
	}
	public IOPResultDto<IObjectVO> addGoodsPurposeMaps(List<GoodsPurposeMapVO> goodsPurposeMapVOList) {
		IOPResultDto<IObjectVO> dto = new IOPResultDto<IObjectVO>();
		try {
			for (GoodsPurposeMapVO goodsPurposeMapVO : goodsPurposeMapVOList) {
				GoodsPurposeMap goodsPurposeMap = new GoodsPurposeMap();
				BeanUtils.copyProperties(goodsPurposeMapVO, goodsPurposeMap);
				Goods goods = iGoodsDao.findById(goodsPurposeMapVO.getGoodsVO().getGoodsId());
				goodsPurposeMap.setGoods(goods);
				Purpose purpose = iPurposeDao.findById(goodsPurposeMapVO.getPurposeVO().getPurposeId());
				goodsPurposeMap.setPurpose(purpose);
				iGoodsPurposeMapDao.add(goodsPurposeMap);
			}
			dto.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("【addGoodsPurposeMaps】"+MyErrorMsgConstants.FAILED_TO_ADD_OPERATE);
			dto.setErrorMSG(MyErrorMsgConstants.FAILED_TO_ADD_OPERATE);
			dto.setSuccess(false);
		}
		return dto;
	}
	
	public IOPResultDto<IObjectVO> deleteGoodsPurposeMaps(List<GoodsPurposeMapVO> goodsPurposeMapVOList) {
		IOPResultDto<IObjectVO> dto = new IOPResultDto<IObjectVO>();
		try {
			for (GoodsPurposeMapVO goodsPurposeMapVO : goodsPurposeMapVOList) {
				GoodsPurposeMap goodsPurposeMap = new GoodsPurposeMap();
				BeanUtils.copyProperties(goodsPurposeMapVO, goodsPurposeMap);
				Goods goods = iGoodsDao.findById(goodsPurposeMapVO.getGoodsVO().getGoodsId());
				goodsPurposeMap.setGoods(goods);
				Purpose purpose = iPurposeDao.findById(goodsPurposeMapVO.getPurposeVO().getPurposeId());
				goodsPurposeMap.setPurpose(purpose);
				iGoodsPurposeMapDao.delete(goodsPurposeMap);
			}
			dto.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("【deleteGoodsPurposeMaps】"+MyErrorMsgConstants.FAILED_TO_DELETE_OPERATE);
			dto.setErrorMSG(MyErrorMsgConstants.FAILED_TO_DELETE_OPERATE);
			dto.setSuccess(false);
		}
		return dto;
	}
	
}
